package im.yisme.view.ui.util;

import im.yisme.R;

import java.util.ArrayList;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LyricsAdapter extends BaseAdapter {

	private ArrayList<Map<String, Object>> data1, data2;
	private Context context;


	public LyricsAdapter(Context context, ArrayList<Map<String, Object>> data1,
			                              ArrayList<Map<String, Object>> data2) {
		this.context = context;
		this.data1 = data1;
		this.data2 = data2;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return data1.size()+data2.size()+2;
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		if(position == 0){
			
			convertView = LayoutInflater.from(context).inflate(
					R.layout.list_divider_local, null);
			
		}
		else if(position<data1.size()+1){
			TextView lyricsName = null;
			TextView noteNum = null;
			TextView duration = null;
			
				convertView = LayoutInflater.from(context).inflate(
						R.layout.playlyricslib_listitem_local, null);	
			
			
			
			lyricsName = (TextView) convertView.findViewById(R.id.textView_lyricsName);			
			noteNum = (TextView) convertView.findViewById(R.id.textView_noteNum);			
			duration = (TextView) convertView.findViewById(R.id.textView_duration);
			
			Map<String, Object> map=data1.get(position-1);
			
			lyricsName.setText((String)map.get("lyricsName"));
			noteNum.setText((String)map.get("noteNum"));
			duration.setText((String)map.get("duration"));
		}
		else if(position == data1.size()+1){
						
				convertView = LayoutInflater.from(context).inflate(
						R.layout.list_divider_dowload, null);
			
		}
		else {
			
			TextView lyricsName = null;
			TextView noteNum = null;
			ImageView star1 = null;
			ImageView star2 = null;
			ImageView star3 = null;
			ImageView star4 = null;
			ImageView star5 = null;
			ImageView official = null;
			
					
				convertView = LayoutInflater.from(context).inflate(
					R.layout.playlyricslib_listitem_download, null);				
			
			
			lyricsName = (TextView) convertView.findViewById(R.id.textView_lyricsName);
			noteNum = (TextView) convertView.findViewById(R.id.textView_noteNum);

			

			star1 = (ImageView) convertView.findViewById(R.id.imageView_star1);
			star2 = (ImageView) convertView.findViewById(R.id.imageView_star2);
			star3 = (ImageView) convertView.findViewById(R.id.imageView_star3);
			star4 = (ImageView) convertView.findViewById(R.id.imageView_star4);
			star5 = (ImageView) convertView.findViewById(R.id.imageView_star5);
			official = (ImageView) convertView.findViewById(R.id.imageView_official);
			
			Map<String, Object> map=data2.get(position-data1.size()-2);
			lyricsName.setText((String)map.get("lyricsName"));
			noteNum.setText((String)map.get("noteNum"));
			
			if(map.get("noteNum").equals("")) {
				TextView noteView = (TextView) convertView.findViewById(R.id.textView2);
				noteView.setVisibility(View.INVISIBLE);
			}
			
			int i=(Integer) map.get("star");
			switch (i) {
			case 0:	star5.setVisibility(View.INVISIBLE);	
			case 1: star4.setVisibility(View.INVISIBLE);
			case 2: star3.setVisibility(View.INVISIBLE);
			case 3: star2.setVisibility(View.INVISIBLE);
			case 4: star1.setVisibility(View.INVISIBLE);
			default:
				break;
			}
			
			boolean isOfficial=(Boolean) map.get("official");
			if(!isOfficial) official.setVisibility(View.INVISIBLE);
		}
		
		
		
		return convertView;
	}

}
